package com.act;

import java.util.ArrayList;

import org.apache.http.Header;
import org.apache.http.message.BasicNameValuePair;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.bean.BaseBean;
import com.common.Constant;
import com.common.TextHttpResponseHandler;
import com.control.UserControl;
import com.xike.kyt.R;
import com.xike.util.MyUtil;
import com.xike.util.StringUtil;

public class UpdateUserInfoAct extends BaseAct {

	EditText edt_name;
	String currentNickName = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_update_userinfo);
		Bundle bundle = getIntent().getExtras();
		if (bundle != null && bundle.containsKey("nickName")) {
			currentNickName = bundle.getString("nickName");
		}

		initViews();
	}

	@Override
	protected void initViews() {
		edt_name = (EditText) findViewById(R.id.updateUserInfo_edt_name);
		edt_name.setText(currentNickName);
		edt_name.setSelection(currentNickName.length());
		findViewById(R.id.updateUserInfo_btn_save).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String strName = edt_name.getText().toString().trim();
				if (strName.equals(currentNickName)) {
					finish();
				} else {
					if (StringUtil.isNotNull(strName)) {
						serviceUpdateUserInfo(strName);
					} else {
						mToast.showToast(UpdateUserInfoAct.this, "请输入新的姓名");
					}
				}

			}
		});
		findViewById(R.id.headTitle_tv_back).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	protected void updateViews() {

	}

	private void serviceUpdateUserInfo(final String newName) {
		ArrayList<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("nick_name", newName));
		params.add(new BasicNameValuePair("summary", ""));
		String strUrl = MyUtil.signRequestUrl(Constant.SERVER_URL_USER_UPDATEINFO, params);
		// 网络请求
		client.put(strUrl, new TextHttpResponseHandler(this) {

			@Override
			public void onSuccess(int statusCode, Header[] headers, String responseString) {
				BaseBean result = gson.fromJson(responseString, BaseBean.class);
				if (result != null) {
					if (result.isReturnTrue()) {
						mToast.showToast(UpdateUserInfoAct.this, "修改成功");
						Intent it_result = new Intent(UpdateUserInfoAct.this, UserInfoAct.class);
						it_result.putExtra("nickName", newName);
						UserControl.updateLoginUserName(newName);
						setResult(1, it_result);
						finish();
					} else {
						mToast.showToast(UpdateUserInfoAct.this, result.getError_description());
					}
				} else {
					mToast.showToast(UpdateUserInfoAct.this, getResources().getString(R.string.net_error));
				}
				super.onSuccess(statusCode, headers, responseString);
			}

			@Override
			public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
				mToast.showToast(UpdateUserInfoAct.this, getString(R.string.net_error));
				super.onFailure(statusCode, headers, responseString, throwable);
			}
		});
	}

}
